<?php
//转变时间
function xilujob_date_change($date)
{
    if (!$date) {
        return '';
    }
    $date = (!is_numeric($date)) ? strtotime($date) : $date;
    if (date('Ymd', $date) == date('Ymd', time())) {
        return date('H:i', $date);
    } else if (date('Ymd', $date) == date('Ymd', strtotime('-1 day'))) {
        return "昨天 " . date('H:i', $date);
    } else if (date('Y', $date) == date('Y', time())) {
        return date('m月d日', $date);
    } else {
        return date('Y年m月d日', $date);
    }
}
//转变时间几  前
function xilujob_time_tran($show_time)
{
    if (!$show_time) {
        return '';
    }
    $show_time = (!is_numeric($show_time)) ? strtotime($show_time) : $show_time;
    $now_time = time();
    $dur = $now_time - $show_time;
    if ($dur < 0) {
        return $show_time;
    } else {
        if ($dur < 10) {
            return '刚刚';
        } else if ($dur < 60) {
            return $dur . '秒前';
        } else {
            if ($dur < 3600) {
                return floor($dur / 60) . '分钟前';
            } else {
                if ($dur < 86400) {
                    return floor($dur / 3600) . '小时前';
                } else {
                    return xilujob_date_change($show_time);
                }
            }
        }
    }
}
function xilujob_time_cha($s_time,$e_time)
{
         $cha = $s_time-$e_time;
         
         //年
         $year = floor($cha  / (31536000));
         if($year>=1){
            return $year."年前";
         }
 
         //月
         $month_total = floor($cha  / 2592000);
         if($month_total>=1){
            return $month_total."月前";
         }
         //日
         $day_total = floor($cha /86400);
        if($day_total>=1){
            return $day_total."天前";
         }
         //时
         $hour_total = floor($cha/3600 );//总相差N小时
        if($hour_total>=1){
            return $hour_total."小时前";
         }
         //分
         $minute_total = floor($cha/60);
        if($minute_total>=1){
            return $minute_total."分钟前";
         }
         //秒
         $second_total = floor($cha);
        if($second_total>=1){
            return $second_total."秒前";
         }
         return '刚刚';
}

/*
* 根据经纬度返回地址信息
*/
function xilujobGetAddr($lat, $lng){
    $config = get_addon_config('address');
    $address = [];
    if ($config['maptype'] == 'baidu') {
        $return = \fast\Http::get("https://api.map.baidu.com/reverse_geocoding/v3/", [
            'location' => "$lat,$lng",
            'coordtype'=> "gcj02ll",
            'ret_coordtype'=>   'gcj02ll',
            'extensions_poi'=> 1,
            'output' => 'json',
            'ak' => $config['baidukey'],
        ]);
        $return = json_decode($return, true);
        if ($return['status'] === 0) {
            $address = [
                'prov' => $return['result']['addressComponent']['province'],
                'city' => $return['result']['addressComponent']['city'],
                'district' => $return['result']['addressComponent']['district'],
            ];
        }
    } else if ($config['maptype'] == 'tencent') {
        $return = \fast\Http::get("https://apis.map.qq.com/ws/geocoder/v1/", [
            'location' => "$lat,$lng",
            'get_poi'   =>  1,
            'key' => $config['tencentkey'],
        ]);
        $return = json_decode($return, true);
        if ($return['status'] === 0) {
            $address = [
                'prov' => $return['result']['address_component']['province'],
                'city' => $return['result']['address_component']['city'],
                'district' => $return['result']['address_component']['district'],
            ];
        }
    } else {
        $return = \fast\Http::get("https://restapi.amap.com/v3/geocode/regeo", [
            'location' => "$lng,$lat",
            'extensions'   => 'all',
            'key' => $config['amapkey'],
        ]);
        $return = json_decode($return, true);
        if ($return['status'] == '1') {
            $address = [
                'prov' => $return['regeocode']['addressComponent']['province'],
                'city' => ($return['regeocode']['addressComponent']['city'] && !empty($return['regeocode']['addressComponent']['city'])) ? $return['regeocode']['addressComponent']['city'] : $return['regeocode']['addressComponent']['province'],
                'district' => $return['regeocode']['addressComponent']['district'],
            ];
        }
    }
    return $address;
}